package com.qdlc.p2p.biz.processor.task.notices;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qd.p2p.adapter.AdaptedRequestHandleService;
import com.qd.p2p.adapter.enums.ResponseOrderStatus;
import com.qd.p2p.adapter.response.QueryOrderStatusResponse;
import com.qd.p2p.baofoo.enums.P2pBizType;
import com.qdlc.p2p.biz.processor.event.TenderNoticeEvent;
import com.qdlc.p2p.biz.processor.task.AbstractTaskProcessor;
import com.qdlc.p2p.biz.service.CollectPayService;
import com.qdlc.p2p.common.constant.TradeStatus;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.common.core.queue.annotation.ScheduledProcessor;
import com.qdlc.p2p.common.util.LogFactory;
import com.qdlc.p2p.dal.dto.TppTradeLog;

/**
 * PS 投标代收
 *
 * @author wuaj
 */
@Service
@ScheduledProcessor(delay = 1, step = 10)
public class TenderNoticeProcessor extends AbstractTaskProcessor {
    protected static final Logger logger = LogFactory.TRADE_LOGGER;

    @Resource
    private CollectPayService borrowTenderCollectService;

    @Resource
    private AdaptedRequestHandleService adaptedPayService;

    @Override
    public boolean supports(ValueEvent event) {
        return event instanceof TenderNoticeEvent;
    }

    @Override
    public void process0(ValueEvent event) throws TaskProcessorException {
        TenderNoticeEvent noticeEvent = (TenderNoticeEvent) event;

        TradeStatus tradeStatus = noticeEvent.getTradeStatus(); // 交易状态
        double amount = noticeEvent.getDepositAmount(); // 投标金额
        TppTradeLog tppTradeLog = noticeEvent.getTppTradeLog(); // 交易log

        // 订单回查
        if (tradeStatus == TradeStatus.TRADE_STATUS_UN_KNOW) {
            QueryOrderStatusResponse response = adaptedPayService.queryOrderStatus(tppTradeLog.getOrderNo(), P2pBizType.TENDER.getType());
            ResponseOrderStatus orderStatus = response.getOrderStatus();
            if (orderStatus == ResponseOrderStatus.NO_ORDER || orderStatus == ResponseOrderStatus.FAILURE) {
                tradeStatus = TradeStatus.TRADE_STATUS_FAILED;
            } else if (orderStatus == ResponseOrderStatus.SUCCESS) {
                tradeStatus = TradeStatus.TRADE_STATUS_FINISHED;
            } else {
                // 状态还是未知 则抛出错误 重新调用
                throw new TaskProcessorException(event);
            }
        }

        String tradeStatusStr = tradeStatus.getStatus();
        logger.debug(MessageFormat.format("S 投标通知处理 -> 订单号:{0} 交易状态:{1} 交易金额:{2}", tppTradeLog.getOrderNo(), tradeStatusStr, amount));
        // 已确定状态 处理业务逻辑
        if (tradeStatus == TradeStatus.TRADE_STATUS_FINISHED) {
            borrowTenderCollectService.tradeFinish(tppTradeLog, tradeStatusStr, amount);
        } else if (tradeStatus == TradeStatus.TRADE_STATUS_FAILED) {
            borrowTenderCollectService.tradeFail(tppTradeLog, tradeStatusStr);
        }
        logger.debug(MessageFormat.format("E 投标通知处理 -> 订单号:{0} 交易状态:{1} 交易金额:{2}", tppTradeLog.getOrderNo(), tradeStatusStr, amount));
    }
}